package com.oddfar.campus.business.service.Impl;

import com.oddfar.campus.business.domain.Wzxx;
import com.oddfar.campus.business.mapper.WzxxMapper;
import com.oddfar.campus.common.domain.PageResult;
import com.oddfar.campus.common.exception.ServiceException;
import com.oddfar.campus.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.Date;
import java.util.List;
import java.util.UUID;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oddfar.campus.business.mapper.WzxxMapper;
import com.oddfar.campus.business.domain.Wzxx;
import com.oddfar.campus.business.service.WzxxService;
import org.springframework.web.multipart.MultipartFile;

@Service
public class WzxxServiceImpl extends ServiceImpl<WzxxMapper, Wzxx> implements WzxxService{

    @Autowired
    private WzxxMapper wzxxMapper;

    @Override
    public PageResult<Wzxx> page(Wzxx wzxx) {
        Long userId = SecurityUtils.getUserId();
        Long phonenumber = wzxxMapper.selectdjxhByuserid(userId);
        if (userId != 1) {
            return wzxxMapper.selectPage(wzxx, phonenumber);
        }
        return wzxxMapper.selectPage(wzxx);
    }
    @Override
    public PageResult<Wzxx> clpage(Wzxx wzxx) { //此方法用于违章处理
        Long userId = SecurityUtils.getUserId();
        Long djxh = wzxxMapper.selectdjxhByuserid(userId);
        if (userId != 1) {
            return wzxxMapper.selectclPage(wzxx, djxh);
        }
        return wzxxMapper.selectPage(wzxx);
    }

    @Override
    public int insertWzxx(Wzxx wzxx) {
        wzxx.setUuid(UUID.randomUUID().toString().toLowerCase());
        try {
            if (wzxx.getDjxh() == null || wzxx.getDjxh().toString().length() < 1) {
                long djxh = wzxxMapper.selectDjxhByMobile(wzxx.getPhonenumber(), wzxx.getClpzh());
                wzxx.setDjxh(djxh);
            }
        } catch (Exception e) {
            throw new ServiceException("新增违章失败：未找到" + wzxx.getPhonenumber()+":"+ wzxx.getClpzh() + "此信息对应的车辆，请先添加该车辆");
        }
        wzxx.setClbz("0");
        return wzxxMapper.insert(wzxx);
    }

    @Override
    public int updateWzxx(Wzxx wzxx) {
        return wzxxMapper.updateById(wzxx);
    }

    @Override
    public boolean updateUserAvatar(String pic) {
        return wzxxMapper.updateUserAvatar(pic) > 0;
    }


    public java.sql.Date getNowTime() {
        long time = System.currentTimeMillis();
        return new Date(time);
    }


}
